<template>
  <div class="app-container">
    <el-form ref="formSearch" :model="searchForm" label-width="100px">
      <el-row>
        <el-col :span="19">
          <el-row class="form_table">
            <el-col :span="8">
              <el-form-item :label="$t('systemRules.ruleCode')" prop="ruleCode">
                <el-input v-model.trim="searchForm.ruleCode" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item :label="$t('systemRules.ruleName')" prop="ruleName">
                <el-input v-model.trim="searchForm.ruleName" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item :label="$t('systemRules.enabled')" prop="enabled">
                <el-select v-model="searchForm.enabled" :placeholder="$t('placeholder.select')" clearable>
                  <el-option v-for="item in $enableDict" :key="item.id" :label="item.text" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="5" style="text-align: center">
          <el-button
            type="primary"
            @click="
              searchForm.pageNumber = 1
              getData()
            "
          >{{ $t('btn.query') }}</el-button>
          <el-button type="danger" @click="resetForm('formSearch')">{{ $t('btn.reset') }}</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-button type="primary" icon="el-icon-plus" @click="openCreateUser">{{ $t('btn.add') }}</el-button>
    <el-button type="primary" icon="el-icon-edit" :disabled="selectedList.length != 1" @click="editDetail('edit', null)">{{ $t('btn.edit') }}</el-button>
    <delete-btn :disabled="selectedList.length < 1" @handleDelete="handleDelete" />
    <el-table stripe :data="tableList" border @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="50" center />
      <el-table-column prop="ruleCode" :label="$t('systemRules.ruleCode')" min-width="110" align="center" />
      <el-table-column prop="ruleName" :label="$t('systemRules.ruleName')" min-width="110" align="center" />
      <el-table-column prop="enabled" :label="$t('systemRules.enabled')" min-width="90" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.enabled ? $t('businessGlossary.enabled1') : $t('businessGlossary.enabled0') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" :label="$t('businessGlossary.creationTime')" align="center" min-width="140" />
      <el-table-column prop="createByView" :label="$t('businessGlossary.creator')" align="center" min-width="100" />
      <el-table-column prop="updateTime" :label="$t('businessGlossary.modifyTime')" align="center" min-width="140" />
      <el-table-column prop="createByView" :label="$t('businessGlossary.modifyUser')" align="center" min-width="100" />
    </el-table>
    <el-pagination v-show="total > 0" background :current-page.sync="searchForm.pageNumber" :page-sizes="$pageSizeAll" :page-size="searchForm.pageSize" layout="total, prev, pager, next, jumper, sizes" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>
<script>
import { rulesPageList, rulesSave, rulesUpdate, rulesExecute, deleteRules } from '@/api/system-set'
export default {
  name: 'Rules',
  data() {
    return {
      id: '',
      executeResult: '',
      treeDialog: false,
      selectedList: [],
      searchForm: {
        ruleCode: null,
        ruleName: null,
        enabled: null,
        pageNumber: 1,
        pageSize: 10,
      },
      tableList: [],
      total: 0,
      dialogTittle: 'view',
      basicDialog: false,
      dialogForm: {
        ruleCode: null,
        ruleName: null,
        ruleContent: null,
        ruleFields: null,
        ruleDemo: {},
        enabled: 1,
      },
      formRules: {
        ruleCode: [{ required: true, message: this.$t('placeholder.input'), trigger: 'blur' }],
        ruleName: [{ required: true, message: this.$t('placeholder.input'), trigger: 'blur' }],
        ruleContent: [{ required: true, message: this.$t('placeholder.input'), trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getData()
  },
  activated() {
    this.getData()
  },
  methods: {
    onJsonChange() {
      // json数据改变时触发
    },
    // 提交按钮
    // confirm() {
    //   this.$refs.ruleForm.validate(async(valid, obj) => {
    //     if (valid) {
    //       const formData = Object.assign({}, this.dialogForm)
    //       formData.ruleDemo = JSON.stringify(formData.ruleDemo)
    //       if (this.dialogTittle == 'add') {
    //         const { code } = await rulesSave(formData)
    //         if (code != '200') return
    //         this.closeDialog(true)
    //       } else {
    //         const { code } = await rulesUpdate(formData)
    //         if (code != '200') return
    //         this.closeDialog(true)
    //       }
    //     } else {
    //       return
    //     }
    //   })
    // },
    // 关闭弹窗
    closeDialog(bool) {
      bool && this.getData() // 点确定关闭弹窗的时候才会刷新列表
      this.$refs['ruleForm'].resetFields()
      this.dialogForm.ruleDemo = {}
      this.dialogForm.ruleContent = ''
      this.executeResult = ''
      this.basicDialog = false
    },
    async execute() {
      const executeData = Object.assign({}, this.dialogForm)
      executeData.ruleDemo = JSON.stringify(this.dialogForm.ruleDemo)
      const { code, data } = await rulesExecute(executeData)
      console.log(code)
      if (code == '200') {
        this.executeResult = data
      }
    },
    // 删除操作
    async handleDelete() {
      const ids = this.selectedList.map((item) => item.id)
      console.log(ids)
      const { code } = await deleteRules(ids)
      // const { code } = await deleteUser(this.selectedList[0].id) // 暂时保留单删接口
      if (code != '200') return
      this.getData()
    },
    // 新建操作
    openCreateUser() {
      this.$router.push({
        path: '/system-set/rules/add',
      })
    },
    // 编辑和查看操作
    editDetail(title, row) {
      this.$router.push({
        path: '/system-set/rules/edit',
        query: {
          id: this.selectedList[0].id,
          accessKey: this.selectedList[0].accessKey,
        },
      })
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.tableList = []
      this.searchForm.pageNumber = 1
      this.total = 0
      this.getData()
    },
    // 查询
    async getData() {
      const params = {
        sort: 'update_time',
        order: 'DESC',
        ...this.searchForm,
      }
      const { data, code } = await rulesPageList(params)
      if (code != '200') return
      this.tableList = data.records
      this.total = data.total
    },
    // 选择项改变时
    handleSelectionChange(val) {
      this.selectedList = val
    },
    // 页码改变
    handleCurrentChange(pageNumber) {
      this.searchForm.pageNumber = pageNumber
      this.getData()
    },
    // 每页size改变时
    handleSizeChange(val) {
      this.searchForm.pageNumber = 1
      this.searchForm.pageSize = val
      this.getData()
    },
  },
}
</script>

<style>
.jsoneditor-poweredBy {
  display: none;
}
</style>
